var orderPage = avalon.define({
	$id: "orderPage",
	uid: "",
	ids: [],
	outDate: "",
	customerPo: "",
	invoiceNo: "",
	sendQty: 0,
	signItems: [],
	isReceipted: 0,
	type:"",
	po:"",
	ci:"",
	outDateParam:"",
	loadData: function() {
		var status = '0';
		if(orderPage.isReceipted == 1) {
			status = '1';
		}
		mui.ajax("/order/orderdetail.json", {
			data: {
				"status": status,
				"type":orderPage.type,
				"po":orderPage.po,
				"ci":orderPage.ci,
				"outDate":orderPage.outDateParam
			},
			success: function(data) {
				if(data) {
					orderPage.outDate = data.head.outDate;
					orderPage.customerPo = data.head.customerPo;
					orderPage.invoiceNo = data.head.invoiceNo;
					orderPage.sendQty = data.head.sendQty;
					orderPage.signItems = data.body;
				} else {
					mui.alert("ERROR");
				}
			}
		});
	}
});

mui.ready(function() {
	
	var uid = $("#uid").val();
	$("input[name='selectAll']").removeAttr("checked");
	orderPage.uid = uid;
	orderPage.isReceipted = $.getParameter("isReceipted");
	orderPage.type = $.getParameter("type");
	orderPage.po = $.getParameter("po");
	orderPage.ci = $.getParameter("ci");
	orderPage.outDateParam = $.getParameter("outDate");
	
	orderPage.loadData();
	
	mui(".order-item").on('change', "input[name='selectAll']", function() {
		if(this.checked) {
			$("input[name='items']").each(function() {
				this.checked = true;
			});
		} else {
			$("input[name='items']").not(':disabled').each(function() {
				this.checked = false;
			});
		}
	});
	
	mui(".mui-card").on('tap', "input[class='itemnum']", function() {
		var height = document.documentElement.clientHeight || document.body.clientHeight;
		document.getElementById("order-pdp").style.height = height +"px";
	});
	
	mui(".receipt").on('tap', 'a', function() {
		$.each($('input[name="items"]:checked').not(':disabled'), function() {
			orderPage.ids.push($(this).val());
		});

		if(orderPage.ids.length == 0) {
			var msg = "The Order Has Finished";
			$.each($('input[name="items"]'), function() {
				if(!$(this).prop('disabled')) {
					msg = "Please Select Item!";
				}
			});
			mui.alert(msg);
		} else {
			var flag = true;
			var reg =  /^[1-9]\d*$/;
			$.each($('input[name="items"]'), function() {
				var itemid = $(this).val();
				var inputval = $("#"+itemid+"").val();
				if(!reg.test(inputval)) {
					flag = false;
				}
			});
			
			if(!flag) {
				mui.alert("Please input a positive integer !");
				return false;
			}
			
			var errmsg = "";
			if(flag){
				$.each($('input[name="items"]'), function() {
					var itemid = $(this).val();
					var norm = $(this).attr("norm");
					var sendQty = $(this).attr("sendQty");
					var receiptedQty = $(this).attr("receiptedQty");
					var inputval = $("#"+itemid+"").val();
					if(inputval > (sendQty-receiptedQty)) {
						flag = false;
						errmsg += "        Norm : " + norm + "\n" +
						          "     sendQty : " + sendQty + "\n" +
						          "receiptedQty : " + receiptedQty + "\n\n"
					}
				});
			}
			
			if(!flag) {
				mui.alert(errmsg + "\n" + "The number you input should be less than unreceipted quantity!");
				return false;
			}
			
			$(".receipt-confirm").show();
		}
	});

	mui(".mui-popup-buttons").on('tap', '.cancel', function() {
		$(".receipt-confirm").hide();
	});

	mui(".mui-popup-buttons").on('tap', '.ok', function() {
		var param = [];
		var idsArr = orderPage.ids;
		for(var i=0;i<idsArr.length;i++) {
			var inputval = $("#"+idsArr[i]+"").val();
			param.push(idsArr[i] + "#" + inputval);
		}
		mui.ajax("/order/signItem.do", {
			data: {
				"ids": param
			},
			success: function(data) {
				$(".receipt-confirm").hide();
				mui.alert(data.msg);
				orderPage.ids = [];
				orderPage.loadData();
				mui.openWindow({
					url: "list.html"
				});
			}
		});
	});

});